﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.DBUtility;
using Microhard.Core;
using Microhard.WarehouseInsp.Buz.Msg;
using Microhard.WarehouseInsp.Buz.Models;
using Microhard.WarehouseInsp.Buz.Interfaces;
using Microhard.WarehouseInsp.Buz.JHWorkflow;

namespace Microhard.WarehouseInsp.Buz.Impls
{
    public class C6WFConfigImpl : IWorkflowConfig
    {
        public Core.DBUtility.ExecutedResult Add(JHFlowInfo model)
        {
            ExecutedResult rtn = new ExecutedResult();
            C6Entities db = new C6Entities();
            MH_OtherSysWorkflowConfig mh = new MH_OtherSysWorkflowConfig();
            mh.AppID = model.AppID;
            mh.DJSN = model.DJSN;
            mh.WorkflowTypeCode = model.WorkflowTypeCode;
            mh.DJAPPIDForSelect = model.DJAPPIDForSelect;
            mh.OtherSysWFID = Guid.NewGuid();
            db.MH_OtherSysWorkflowConfig.AddObject(mh);
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, JHFlowInfoMsg.AddFailed);
                Log.Error("IWorkflowConfig.Add", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(JHFlowInfo model)
        {
            ExecutedResult result = new ExecutedResult();
            C6Entities db = new C6Entities();
            try
            {
                var entity = db.MH_OtherSysWorkflowConfig.SingleOrDefault(l => l.OtherSysWFID == model.OtherSysWFID);
                if (entity != null)
                {
                    entity.DJSN = model.DJSN;
                    entity.AppID = model.AppID;
                    entity.WorkflowTypeCode = model.WorkflowTypeCode;
                }
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, JHFlowInfoMsg.UpdateFailed);
                Log.Error("IWorkflowConfig.Update", ex);
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            C6Entities db = new C6Entities();
            var entity = db.MH_OtherSysWorkflowConfig.Where(l => ids.Contains(l.OtherSysWFID));
            foreach (var e in entity)
            {
                db.MH_OtherSysWorkflowConfig.DeleteObject(e);
            }
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, JHFlowInfoMsg.DeleteFailed);
                    Log.Error("IWorkflowConfig.Delete", ex);
                }
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(JHFlowInfo model)
        {
            throw new NotImplementedException();
        }

        public JHFlowInfo GetModelByID(Guid id)
        {
            C6Entities db = new C6Entities();
            var mcmp = db.MH_OtherSysWorkflowConfig.SingleOrDefault(c => c.OtherSysWFID == id);
            if (mcmp != null)
            {
                return new JHFlowInfo
                {
                    AppID = mcmp.AppID,
                    DJSN = mcmp.DJSN,
                    OtherSysWFID = mcmp.OtherSysWFID,
                    WorkflowTypeCode = mcmp.WorkflowTypeCode,
                    DJAPPIDForSelect = mcmp.DJAPPIDForSelect
                };
            }
            return null;
        }

        public JHFlowInfo GetModelByModel(JHFlowInfo model)
        {
            throw new NotImplementedException();
        }

        public List<JHFlowInfo> Query(JHFlowInfo Model, int pageIndex, int pageSize, out int totalCount)
        {
            C6Entities db = new C6Entities();
            IEnumerable<MH_OtherSysWorkflowConfig> entities = from b in db.MH_OtherSysWorkflowConfig
                                                              select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.WorkflowTypeCode).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => new JHFlowInfo
                {
                    AppID = m.AppID,
                    DJSN = m.DJSN,
                    OtherSysWFID = m.OtherSysWFID,
                    WorkflowTypeCode = m.WorkflowTypeCode,
                    DJAPPIDForSelect = m.DJAPPIDForSelect
                }).ToList();
            return new List<JHFlowInfo>();
        }

        public List<JHFlowInfo> Query(JHFlowInfo Model)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }

        public bool IsDJDuplicate(JHFlowInfo model)
        {
            C6Entities db = new C6Entities();
            var cnt = db.MH_OtherSysWorkflowConfig.Count(c => c.DJSN == model.DJSN && model.OtherSysWFID != c.OtherSysWFID);
            return cnt > 0 ? true : false;
        }


        public bool IsAppIDDuplicate(JHFlowInfo model)
        {
            C6Entities db = new C6Entities();
            var cnt = db.MH_OtherSysWorkflowConfig.Count(c => c.AppID == model.AppID && model.OtherSysWFID != c.OtherSysWFID);
            return cnt > 0 ? true : false;
        }


        public bool IsWorkflowTypeCodeDuplicate(JHFlowInfo model)
        {
            C6Entities db = new C6Entities();
            var cnt = db.MH_OtherSysWorkflowConfig.Count(c => c.WorkflowTypeCode == model.WorkflowTypeCode && model.OtherSysWFID != c.OtherSysWFID);
            return cnt > 0 ? true : false;
        }
    }
}
